<?php

use app\admin\model\Admin;

if(!function_exists('get_tree')){
    function get_tree($data, $parent_id_value = 0, $id = 'id', $parent_id = 'parent_id', $children = 'children')
    {
        $arr = [];
        foreach ($data as $k => $v) {
            if ($v[$parent_id] == $parent_id_value) {
                $v[$children] = get_tree($data, $v[$id]);
                $arr[] = $v;
            }
        }
        return $arr;
    }
}

if (!function_exists('get_permission')){
    function get_permission(int $id){
        $admin = Admin::find($id);
        $arr = [];
        $roles = $admin->roles->where('status',1);
        foreach ($roles as $role){
            foreach ($role->permissions->where('status',1)->where('show',1) as $permission){
                $arr[] = $permission;
            }
        }
        return array_unique($arr);
    }
}